package com.yanglun.mvp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yanglun.mvp.presenter.UserPresenter;
import com.yanglun.mvp.view.IUserView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener,IUserView {
    private UserPresenter presenter;
    private EditText id,first,last;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.save).setOnClickListener(this);
        findViewById(R.id.load).setOnClickListener(this);
        findViewById(R.id.mvc).setOnClickListener(this);
        id = (EditText) findViewById(R.id.id);
        first = (EditText) findViewById(R.id.first);
        last = (EditText) findViewById(R.id.last);
        presenter = new UserPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                presenter.saveUser(this);
                break;
            case R.id.load:
                presenter.loadUser(this,getID());
                break;
            case R.id.mvc:
                this.startActivity(new Intent(this,MVCActivity.class));
                break;
            default:
                break;
        }
    }


    @Override
    public int getID() {
        try{
            return new Integer(id.getText().toString());
        }catch (Exception e){
            return -1;
        }

    }

    @Override
    public String getFristName() {
        return first.getText().toString();
    }

    @Override
    public String getLastName() {
        return last.getText().toString();
    }

    @Override
    public void setFirstName(String firstName)
    {
        first.setText(firstName);
    }

    @Override
    public void setLastName(String lastName) {
        last.setText(lastName);
    }

    @Override
    public void setID(int id) {
        
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }


}